//记录每个点的属性
var starObj ={
    x:0,//x
    y:0,//y
    g:0,//离原点距离
    h:0,//离到达点距离
    f:0,//综合距离
    parent:null,//父节点
};


cc.Class({
    extends: cc.Component,

    properties: {
        B:cc.Node,
    },

    // use this for initialization
    onLoad: function () {
        this.map =[
            [0,0,0,0,0,0,1,0,0],
            [0,0,0,0,1,0,1,0,0],
            [0,0,0,0,1,0,1,0,0],
            [0,0,0,0,1,0,1,0,0],
            [0,1,1,1,1,0,1,0,0],
            [0,0,0,0,1,0,1,0,0],
            [0,0,0,0,1,0,1,0,0],
            [0,0,0,0,1,0,1,0,0],
            [0,0,0,0,1,0,0,0,0],
        ];
        this.aIndex = cc.v2(2,8);//要去的点
        this.bIndex = cc.v2(7,2);//本身的点
        this.time = 500;//寻路500次找不到就不找了

        this.closeArr = [];
        this.openArr = [];
    },
    //曼哈顿距离
    manDis:function(fromV,toV){
        var disx = Math.abs(fromV.x - toV.x);
        var disy = Math.abs(fromV.y-toV.y);
        return disx+disy;
    },
    isInClose:function(x,y){
        for (let index = 0; index < this.closeArr.length; index++) {
            const element = this.closeArr[index];
            if(element.x == x && element.y == y){
                return true;
            }
        }
        return false;
    },
    isInOpen:function(x,y){
        for (let index = 0; index < this.openArr.length; index++) {
            const element = this.openArr[index];
            if(element.x == x && element.y == y){
                return true;
            }
        }
        return false;
    },
    //寻找周围点
    aroundPos:function(obj){
        var arr = [[1,0],[0,1],[-1,0],[0,-1]];
        for(let i=0;i<arr.length;i++){
            var mx = obj.x + arr[i][0];
            var my = obj.y + arr[i][1];
            if(mx <0 || mx >=9 || my<0 ||my>=9){
                //超出界限
                continue;
            }
            if(this.map[mx][my] == 1){
                //该点为障碍
                continue;
            }
            if(this.isInClose(mx,my)){
                continue;
            }
            if(this.isInOpen(mx,my)){
                continue;
            }
            var sIndex = cc.v2(mx,my);
            var astar = new Object();
            astar.x = mx;
            astar.y = my;
            astar.h = this.manDis(sIndex,this.bIndex);
            astar.g = this.manDis(sIndex,this.aIndex);
            astar.f = astar.h+astar.g;
            astar.parent = obj;
            //放入open
            this.openArr.push(astar);
        }
    },
    //寻找open中距离最小的点
    findMixAround:function(){
        var min = 999;
        var findIndex = 0;
        for (let index = 0; index < this.openArr.length; index++) {
            const element = this.openArr[index];
            if(element.f < min){
                min = element.f;
                findIndex = index;
            }
        }
        //从open删除该点
        var findObj = this.openArr.splice(findIndex,1);
        //装入close中
        this.closeArr.push(findObj[0]);
        return findObj[0];
    },
    aStar:function(){
        var astar = new Object();
        astar.x = this.bIndex.x;
        astar.y = this.bIndex.y;
        astar.h = this.manDis(this.bIndex,this.bIndex);
        astar.g = this.manDis(this.bIndex,this.aIndex);
        astar.f = astar.h+astar.g;
        astar.parent = null;
        //放入close
        this.closeArr.push(astar);

        var tmp = astar;
        while(true){
            this.time --;
            //寻找周围一圈的点
            this.aroundPos(tmp);
            tmp = this.findMixAround();
            if(tmp.x == this.aIndex.x&&tmp.y == this.aIndex.y){
                //找到了
                break;
            }

            if(this.time<=0){
                console.log("超过步长");
                break;
            }

        }
        //console.log(this.closeArr);
        //最终找到的点在close中
        var p = this.closeArr[this.closeArr.length-1];
        this.final = [];
        while(p){
            this.final.push(p);
            p = p.parent;
        }
        this.final.reverse();//翻转数组
        //沿着数组走
        console.log(this.final);
        this.gogo(0);
    },
    //转换坐标
    croverPos:function(x,y){
        //0,0 ->-400,400
        var posx = -400+100*y;
        var posy = 400-100*x;
        return cc.v2(posx,posy);
    },
    gogo:function(index){
        this.B.runAction(cc.sequence(cc.moveTo(0.5,this.croverPos(this.final[index].x,this.final[index].y)),cc.callFunc(()=>{
            index++;
            if(index >= this.final.length){

            }else{
                this.gogo(index);
            }
        })));
    },
    start(){
        this.aStar();
    },

    // called every frame
    update: function (dt) {

    },
});
